#include <stdio.h>
#include <string.h>

#define MAXLINE 100
#define abs(x) ((x) > 0 ? (x): -(x))

void itoa(int n, char s[], int width);
void reverse(char s[]);

int main() {

    int num, width;
    char str[MAXLINE];

    num = -123;
    width = 8;

    itoa(num, str, width);

    printf("%s\n", str);

    return 0;
}

void itoa(int n, char s[], int width) {
    int i, tmp;
    i = 0;

    tmp = n;

    while (tmp != 0) {
        s[i++] = abs(tmp % 10) + '0';
        tmp /= 10;
    }

    if (n < 0) {
        s[i++] = '-';
    }

    while (i < width) {
        s[i++] = ' ';
    }

    s[i] = '\0';
    reverse(s);
}

void reverse(char s[]) {
    int i, j, tmp;
    for (i = 0, j = strlen(s) - 1; i < j; ++i, --j) {
        tmp = s[i];
        s[i] = s[j];
        s[j] = tmp;
    }
}